package com.duoduo.demo.springnoxml.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.duoduo.demo.springnoxml.dao.HelpCategoryDao;
import com.duoduo.demo.springnoxml.model.HelpCategory;
import com.duoduo.demo.springnoxml.vo.HelpCategoryVO;

@Service
public class HelpCategoryService {

	@Autowired
	private HelpCategoryDao helpCategoryDao;

	public List<HelpCategoryVO> listAll() {
		return fromEntityList(helpCategoryDao.listAll());
	}

	public List<HelpCategoryVO> listRoot() {
		return listByParent(0);
	}

	public List<HelpCategoryVO> listByParent(int parentId) {
		return fromEntityList(helpCategoryDao.listByParent(parentId));
	}

	private List<HelpCategoryVO> fromEntityList(List<HelpCategory> entityList) {
		List<HelpCategoryVO> voList = new ArrayList<HelpCategoryVO>(0);
		if (CollectionUtils.isEmpty(entityList)) {
			return voList;
		}

		for (HelpCategory entity : entityList) {
			voList.add(HelpCategoryVO.fromEntity(entity));
		}
		return voList;
	}
}
